// ubox_recorder_testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ubox_recorder_test.h"
#include "ubox_recorder_testDlg.h"
#include "setbusytonedlg.h"
#include "SetVolumeDlg.h"
          
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define RECOED_DIR    "recorder-dir\\"
CUbox_recorder_testDlg *pusb = NULL;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUbox_recorder_testDlg dialog

CUBoxLine*
CUbox_recorder_testDlg::get_line_by_ID( size_t lineID_ )
{
	CUBoxLine* pLine = NULL;
	
	for( size_t lineID=0; lineID < MAX_DEVICE; lineID++ )
	{
		if( ubox_device[lineID].line_id() == lineID_ )
		{
			pLine = static_cast<CUBoxLine*>( &ubox_device[lineID] );
			break;
		}
	}
	
	return pLine;
}

CUBoxLine*
CUbox_recorder_testDlg::get_line_by_handle( long handle )
{
	CUBoxLine* pLine = NULL;
	
	for( size_t lineID=0; lineID < MAX_DEVICE; lineID++ )
	{
		if( ubox_device[lineID].line_handel() == handle )
		{
			pLine = static_cast<CUBoxLine*>( &ubox_device[lineID] );
			break;
		}
	}
	
	return pLine;
}

CUbox_recorder_testDlg::CUbox_recorder_testDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUbox_recorder_testDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUbox_recorder_testDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUbox_recorder_testDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUbox_recorder_testDlg)
	DDX_Control(pDX, IDC_DISSTATE, m_list);
	DDX_Control(pDX, IDC_DISINFOR, m_dis);
	DDX_Control(pDX, IDC_PHONICUSBCTRL, m_PhonicUSB);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUbox_recorder_testDlg, CDialog)
	//{{AFX_MSG_MAP(CUbox_recorder_testDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDM_CLOSEAGC, OnCloseagc)
	ON_COMMAND(IDM_DIALPHONENUM, OnDialphonenum)
	ON_COMMAND(IDM_GETHANGTHRESHOLD, OnGethangthreshold)
	ON_COMMAND(IDM_GETHARDWAREVERSION, OnGethardwareversion)
	ON_COMMAND(IDM_GETPRODUCTNAME, OnGetproductname)
	ON_COMMAND(IDM_GETUSERNUM, OnGetusernum)
	ON_COMMAND(IDM_OPENBUZZER, OnOpenbuzzer)
	ON_COMMAND(IDM_OPENCONFIGUREMODE, OnOpenconfiguremode)
	ON_COMMAND(IDM_OPENMSGSWITCH, OnOpenmsgswitch)
	ON_COMMAND(IDM_PAUSEPLAY, OnPauseplay)
	ON_COMMAND(IDM_PLAYFILE, OnPlayfile)
	ON_COMMAND(IDM_READEEPROM, OnReadeeprom)
	ON_COMMAND(IDM_READEEPROMEX, OnReadeepromex)
	ON_COMMAND(IDM_READLINEVOLTAGE, OnReadlinevoltage)
	ON_COMMAND(IDM_RECORDFILE, OnRecordfile)
	ON_COMMAND(IDM_RECORDSTREAM, OnRecordstream)
	ON_COMMAND(IDM_SETHANGTHRESHOLD, OnSethangthreshold)
	ON_COMMAND(IDM_SETHOOKOFFTHRESHOLD, OnSethookoffthreshold)
	ON_COMMAND(IDM_SOFTHANGUP, OnSofthangup)
	ON_COMMAND(IDM_SOFTHOOKOFF, OnSofthookoff)
	ON_COMMAND(IDM_STOPPLAY, OnStopplay)
	ON_COMMAND(IDM_STOPREADLINEVOLTAGE, OnStopreadlinevoltage)
	ON_COMMAND(IDM_STOPRECORD, OnStoprecord)
	ON_COMMAND(IDM_WRITEEEPROM, OnWriteeeprom)
	ON_COMMAND(IDM_SETBUSYTONE, OnSetBusyTone)
	ON_COMMAND(IMD_GETHOOKOFFTHRESHOLD, OnGethookoffthreshold)
	ON_NOTIFY(NM_DBLCLK, IDC_DISSTATE, OnDblclkOpt)
	ON_WM_DESTROY()
	ON_COMMAND(IDM_RESTART, OnRestart)
	ON_COMMAND(IDM_SET_VOLUME, OnSetVolume)
	//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUbox_recorder_testDlg message handlers

BOOL CUbox_recorder_testDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
		m_font.CreateFont(16, 0,0,0,FW_NORMAL, 0,0,0,
		DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial");
	m_list.SetFont(&m_font);
	
	
	/*-----------------------------------------------------------*/	
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_list.SetBkColor(RGB(247,247,255));
	m_list.SetTextColor(RGB(0,0,255));
	m_list.SetTextBkColor(RGB(247,247,255));
	m_list.InsertColumn(0, "LineID", LVCFMT_LEFT, 60);
	m_list.InsertColumn(1, "productname", LVCFMT_LEFT, 120);
	m_list.InsertColumn(2, "State", LVCFMT_LEFT, 100);
	m_list.InsertColumn(3, "Key", LVCFMT_LEFT, 120);
	m_list.InsertColumn(4, "CallerNum", LVCFMT_LEFT, 120);
	m_list.InsertColumn(5, "other", LVCFMT_LEFT, 120);
	
	m_PhonicUSB.OpenLogFile(0);
	((CButton *)GetDlgItem(IDC_AUDORECORD))->SetCheck(TRUE); 
	if(m_PhonicUSB.OpenDevice(0))
	{
		m_dis.InsertString(m_dis.GetCount(),"open device error");
		m_dis.SetCurSel(m_dis.GetCount()-1);   
	}
	  pusb = this;
     char  s_szName[MAX_PATH];
    ::GetModuleFileName(NULL, s_szName, MAX_PATH);
	strPath = s_szName;
	
	
	size_t dotPos = strPath.ReverseFind( '\\' );
	if( dotPos != -1 )
	{
	   strPath.Delete( dotPos, strPath.GetLength()- dotPos);
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUbox_recorder_testDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CUbox_recorder_testDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CUbox_recorder_testDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUbox_recorder_testDlg::OnCloseagc() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
        
		long hdl = pLine->line_handel();
		if(pLine->get_agc())
		{
			pLine->set_agc(false);
			m_PhonicUSB.CloseAgc(hdl,false);     //open agc
			
		}
		else
		{
           	pLine->set_agc(true);
            m_PhonicUSB.CloseAgc(hdl,true);     //close agc
			
		}
	}
}

void CUbox_recorder_testDlg::OnDialphonenum() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);

	if(pLine)
	{
		long hdl = pLine->line_handel();
		if( pLine->SendDtmfDlg.DoModal() == IDOK )
		{
		  int res = m_PhonicUSB.GetProductID(hdl);
		  if((res == 1)||(res == 4) )
		  {
			  m_PhonicUSB.SoftHookoff(hdl);
			  Sleep(1000);
		  }

          if(m_PhonicUSB.SendDtmf(hdl,pLine->SendDtmfDlg.m_phonenum) !=0)
			  m_dis.InsertString(m_dis.GetCount(),"dial num failed"); 
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   

}

void CUbox_recorder_testDlg::OnGethangthreshold() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
		char buff[100];
		long hdl = pLine->line_handel();
		int res = m_PhonicUSB.GetHangThreshold(hdl);
		if(res >0)
		{
          sprintf(buff,"Hang threshold:%dV",res);
		  m_dis.InsertString(m_dis.GetCount(),buff);
		}
		else
		{
			if(res == -6)
				m_dis.InsertString(m_dis.GetCount(),"this function work in configure mode");
			else 
				m_dis.InsertString(m_dis.GetCount(),"get hook off threshold failed");
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);  
}

void CUbox_recorder_testDlg::OnGethardwareversion() 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
		char buff[100];
		long hdl = pLine->line_handel();
        sprintf(buff,"hardware ver:V%2.1f",m_PhonicUSB.GetDeviceVersionNum(hdl));
        m_dis.InsertString(m_dis.GetCount(),buff);
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnGetproductname() 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		int res = m_PhonicUSB.GetProductID(hdl);
		if( res>0)
		{
			CString str;
			switch(res)
			{
			case 1:
				str = "FI3102A";
				break;
			case 2:
				str = "FI3002B";
				break;
			case 3:
				str = "FI3001B";
					break;
			case 4:
				str = "FI3101A";
				break;
			default:
				str = "unkown product";
			}
		m_dis.InsertString(m_dis.GetCount(),str);
		}
		else
           	m_dis.InsertString(m_dis.GetCount(),"get product id failed");
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnGetusernum() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;

	if(pLine)
	{  
		long hdl = pLine->line_handel();
		unsigned char num[10];
		res = m_PhonicUSB.GetUsernum(hdl,(short*)num,10);
		if(res >0)
		{
			int i;
			CString str = "USer num:";
			for(i=0;i<res;i++)
                str +=num[i];

			m_dis.InsertString(m_dis.GetCount(),str);
		}
		else
			m_dis.InsertString(m_dis.GetCount(),"get user num failed");
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnOpenbuzzer() 
{
	// TODO: Add your command handler code here
   	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
        
		long hdl = pLine->line_handel();
	    if(pLine->get_buzzerSwitch())
		{
			pLine->set_buzzerSwitch(false);
			m_PhonicUSB.OpenBuzzerSwitch(hdl,false);     //close buzzer
			
		}
		else
		{
           	pLine->set_buzzerSwitch(true);
            m_PhonicUSB.OpenBuzzerSwitch(hdl,true);     //open  buzzer

		}
		m_dis.SetCurSel(m_dis.GetCount()-1);   
	}	
}

void CUbox_recorder_testDlg::OnOpenconfiguremode() 
{
	m_PhonicUSB.CloseDevice();
	m_list.DeleteAllItems();
    	int i;
	for(i=0;i<MAX_DEVICE;i++)
		ubox_device[i].	handl_plug_out();
	if(m_PhonicUSB.OpenDevice(2) == 0)  // 
	{
        m_dis.InsertString(m_dis.GetCount(),"use configure mode open device ok");
	}
	else
         m_dis.InsertString(m_dis.GetCount(),"use configure mode open device failed");
    m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnOpenmsgswitch() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
        
		long hdl = pLine->line_handel();
	    if(pLine->get_MessageSwitch())
		{
			pLine->set_MessageSwitch(false);
			m_PhonicUSB.SetMessageParm(hdl,false);     //close buzzer
			
		}
		else
		{
           	pLine->set_MessageSwitch(true);
            m_PhonicUSB.SetMessageParm(hdl,true);     //open  buzzer

		}
		m_dis.SetCurSel(m_dis.GetCount()-1);   
	}
}

void CUbox_recorder_testDlg::OnPauseplay() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
		int res;
		long hdl = pLine->line_handel();
		if(pLine->bplaypause)
		{
			pLine->bplaypause = false;
            res = m_PhonicUSB.PlayPause(hdl);
		}
		else
		{
			pLine->bplaypause = true;
			res = m_PhonicUSB.PlayPause(hdl);
		}
	
		if(res ==0)
		{

		  m_dis.InsertString(m_dis.GetCount(),"play pause,click this menu continued playing file");
		}
		else
		m_dis.InsertString(m_dis.GetCount(),"set play pause failed");
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);
}

void CUbox_recorder_testDlg::OnPlayfile() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;
	
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		CFileDialog dlg(true);
		if(dlg.DoModal()==IDOK)
		{
			 res = m_PhonicUSB.PlayFile(hdl,dlg.GetPathName());
			if(res != 0)
				m_dis.InsertString(m_dis.GetCount(),"play file  failed");
		}
		m_dis.SetCurSel(m_dis.GetCount()-1);   
	}
}

void CUbox_recorder_testDlg::OnReadeeprom() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;
	
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		unsigned char data[8];
		res = m_PhonicUSB.ReadEeprom(hdl,0,(short*)data,8);
		if(res !=0)
			m_dis.InsertString(m_dis.GetCount(),"read eeprom data failed");
		else
		{
		
			m_dis.InsertString(m_dis.GetCount(),"Read rom data from addr 0-7:");
			char buff[100];
			sprintf(buff,"0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
			m_dis.InsertString(m_dis.GetCount(),buff);
			
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnReadeepromex() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;
	
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		unsigned char data[8];
		res = m_PhonicUSB.ReadEepromEx(hdl,0,(short*)data,8);
		if(res !=0)
			m_dis.InsertString(m_dis.GetCount(),"read eeprom data failed");
		else
		{
			
			m_dis.InsertString(m_dis.GetCount(),"Read eepromdata from addr 0-7:");
			char buff[100];

			sprintf(buff,"0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
			m_dis.InsertString(m_dis.GetCount(),buff);
			
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnReadlinevoltage() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;

	if(pLine)
	{  
		long hdl = pLine->line_handel();
		res = m_PhonicUSB.StartReadLineVoltage(hdl);
		if(res !=0)
			m_dis.InsertString(m_dis.GetCount(),"StartReadLineVoltage failed");
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnRecordfile() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;

	if(pLine)
	{  
		long hdl = pLine->line_handel();
		CString		sFilter = 
			"Wave Files(*.wav)|*.wav";
		CFileDialog	dlgOpen( FALSE,"WAV", 0, OFN_HIDEREADONLY, 
			(LPCTSTR)sFilter, NULL );
		if( dlgOpen.DoModal() == IDOK )
		{
			res = m_PhonicUSB.RecordFile(hdl,(LPCTSTR)(dlgOpen.GetPathName()),0);
			
			if(res !=0)
			{
				
				m_dis.InsertString(m_dis.GetCount(),"record file failed");
			}
		
		}
		m_dis.SetCurSel(m_dis.GetCount()-1);   
	}
}

void CUbox_recorder_testDlg::OnRecordstream() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = get_line_by_ID(m_iClickItemID);

	if(pLine)
	{  
		long hdl = pLine->line_handel();
		CString		sFilter = 
			"Wave Files(*.wav)|*.wav";
		CFileDialog	dlgOpen( FALSE,"WAV", 0, OFN_HIDEREADONLY, 
			(LPCTSTR)sFilter, NULL );
		if( dlgOpen.DoModal() == IDOK )
		{
			pLine->fpstream = fopen((LPCTSTR)(dlgOpen.GetPathName()),"wb+");
			if(!pLine->fpstream)
                m_dis.InsertString(m_dis.GetCount(),"create file failed");
			else
			{  
				WORD fmt = WAVE_FORMAT_ALAW;  //WAVE_FORMAT_MULAW  WAVE_FORMAT_PCM

				WaveFileHeader pcmWavHeader( 0, fmt );
				int res = fwrite((const char*) &pcmWavHeader, sizeof(pcmWavHeader),1,pLine->fpstream);
				if(res != 1)
				{
					m_dis.InsertString(m_dis.GetCount(),"write file head  failed");
						m_dis.SetCurSel(m_dis.GetCount()-1);   
				}
			}
			
			if(m_PhonicUSB.RecordStream(hdl,0) !=0)
			{
				m_dis.InsertString(m_dis.GetCount(),"stream record failed");
				if(pLine->fpstream)
				{
					fclose(pLine->fpstream);
					pLine->fpstream = NULL;
				}
			}
		}
	}
}

void CUbox_recorder_testDlg::OnSethangthreshold() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);

	if(pLine)
	{
		long hdl = pLine->line_handel();
		int value_d = atoi(pLine->threshold.m_hangTh.GetBuffer(pLine->threshold.m_hangTh.GetLength()));
		if( pLine->threshold.DoModal() == IDOK )
		{
          if(m_PhonicUSB.SetHangThreshold(hdl,value_d) !=0)
			  m_dis.InsertString(m_dis.GetCount(),"set hookoff threshold faild"); 
		}
		pLine->threshold.m_hangTh.ReleaseBuffer();
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   

}

void CUbox_recorder_testDlg::OnSethookoffthreshold() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	//setthreshold	threshold;
	if(pLine)
	{
		long hdl = pLine->line_handel();
		int value_d = atoi(pLine->threshold.m_hookoffTh.GetBuffer(pLine->threshold.m_hookoffTh.GetLength()));
		if( pLine->threshold.DoModal() == IDOK )
		{
          if(m_PhonicUSB.GetHookOffThreshold(hdl) !=0)
			  m_dis.InsertString(m_dis.GetCount(),"set hookoff threshold faild"); 
		}
		pLine->threshold.m_hookoffTh.ReleaseBuffer();
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   

}

void CUbox_recorder_testDlg::OnSofthangup() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		if(m_PhonicUSB.SoftHookon(hdl) !=0)
		{
			m_dis.InsertString(m_dis.GetCount(),"soft hookon failed!");
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnSofthookoff() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		if(m_PhonicUSB.SoftHookoff(hdl) !=0)
		{
			m_dis.InsertString(m_dis.GetCount(),"SoftHookoff failed");
		}
	}
}

void CUbox_recorder_testDlg::OnStopplay() 
{
	// TODO: Add your command handler code here
			CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;

	if(pLine)
	{  
		long hdl = pLine->line_handel();
		res = m_PhonicUSB.StopPlay(hdl);
		if(res !=0)
			m_dis.InsertString(m_dis.GetCount(),"stop play file failed");
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);  
}

void CUbox_recorder_testDlg::OnStopreadlinevoltage() 
{
	// TODO: Add your command handler code here
			CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;

	if(pLine)
	{  
		long hdl = pLine->line_handel();
			res =m_PhonicUSB.StopReadLineVoltage(hdl);
		if(res !=0)
			m_dis.InsertString(m_dis.GetCount(),"stop read line voltage failed");
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnStoprecord() 
{
	// TODO: Add your command handler code here
		CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;

	if(pLine)
	{  
		long hdl = pLine->line_handel();
        	res = m_PhonicUSB.StopRecord(hdl);
		if(res !=0)
			m_dis.InsertString(m_dis.GetCount(),"stop recorder file failed");
		
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnWriteeeprom() 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	int res;
	
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		unsigned char data[8] = {170,2,3,4,98,6,7,255};    //1->0x1  255->0xFF  data[i]<256;
		res = m_PhonicUSB.WriteEeprom(hdl,0,(short*)data,8);   //data [0,255]
		if(res !=0)
			m_dis.InsertString(m_dis.GetCount(),"read eepromex data failed");
		else
		{
			m_dis.InsertString(m_dis.GetCount(),"write data ok");
			
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnSetBusyTone() 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		CSetBusyToneDlg Dlg;
		if(Dlg.DoModal()==IDOK)
		{
			m_PhonicUSB.SetBusyToneEx(hdl,Dlg.m_bDualTone,Dlg.m_lFrequency0 ,Dlg.m_Frequencey1 ,Dlg.m_lBusyOn/10 ,Dlg.m_lBusyOff/10 ,Dlg.m_lTimeError/10 ,Dlg.m_lBusyEnergy ,Dlg.m_lBusyCount); 
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
	
}

void CUbox_recorder_testDlg::OnSetVolume() 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	
	if(pLine)
	{  
		long hdl = pLine->line_handel();
		CSetVolumeDlg dlg;
		if(dlg.DoModal()==IDOK)
		{
			m_PhonicUSB.SetSpeakerVolume(hdl,dlg.m_lSpkVol );
			m_PhonicUSB.SetMicVolume(hdl,dlg.m_lMicVol);
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1);   
	

}

void CUbox_recorder_testDlg::OnGethookoffthreshold() 
{
	// TODO: Add your command handler code here
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_ID(m_iClickItemID);
	if(pLine)
	{  
		char buff[100];
		long hdl = pLine->line_handel();
		int res = m_PhonicUSB.GetHookOffThreshold(hdl);
		if(res >0)
		{
          sprintf(buff,"hookoff threshold:%dV",res);
		  m_dis.InsertString(m_dis.GetCount(),buff);
		}
		else
		{
			if(res == -6)
              m_dis.InsertString(m_dis.GetCount(),"this function work in configure mode");
			else 
				m_dis.InsertString(m_dis.GetCount(),"get hook off threshold failed");
		}
	}
	m_dis.SetCurSel(m_dis.GetCount()-1); 
}

void CUbox_recorder_testDlg::OnDblclkOpt(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	m_iClickItemID=m_list.GetSelectionMark();
	  PopOpt(m_iClickItemID);
	*pResult = 0;
}



void  CUbox_recorder_testDlg::PopOpt(int index)
{
   
		//	ClientToScreen (&(m_ClickPoint));

			CMenu	TMenu;
			CMenu * pMenu;

			//CWnd
			RECT	rect;
			m_list.GetWindowRect( &rect );

			if( TMenu.LoadMenu (IDR_MENU_OPT) )
			{
				pMenu = TMenu.GetSubMenu (0);
				CUBoxLine* pLine = NULL;
				pLine = get_line_by_ID(index);
				
				if( pMenu&& pLine)
				{
					if(pLine->get_agc())
						pMenu->CheckMenuItem(IDM_CLOSEAGC,MF_CHECKED);
					else
						pMenu->CheckMenuItem(IDM_CLOSEAGC,MF_UNCHECKED);

					if(pLine->get_buzzerSwitch())
						pMenu->CheckMenuItem(IDM_OPENBUZZER,MF_CHECKED);  
					else
						pMenu->CheckMenuItem(IDM_OPENBUZZER,MF_UNCHECKED);

					if(pLine->get_MessageSwitch())
						pMenu->CheckMenuItem(IDM_OPENMSGSWITCH,MF_CHECKED);
					else
						pMenu->CheckMenuItem(IDM_OPENMSGSWITCH,MF_UNCHECKED);

					if(pLine->bplaypause)
						pMenu->CheckMenuItem(IDM_PAUSEPLAY,MF_CHECKED);
					else
						pMenu->CheckMenuItem(IDM_PAUSEPLAY,MF_UNCHECKED);
			
					pMenu->TrackPopupMenu (TPM_LEFTALIGN|TPM_LEFTBUTTON, 
						100+rect.left, /*m_ClickPoint.y*/100, 
						this, NULL);	
					
				}
			}
}

void CUbox_recorder_testDlg::OnDestroy() 
{
	m_PhonicUSB.CloseDevice();
	m_PhonicUSB.CloseLogFile();
	CDialog::OnDestroy();
	
}



void   CUbox_recorder_testDlg::autoRecordFile(long hdl)
{
}



void CUbox_recorder_testDlg::OnRestart() 
{
	// TODO: Add your command handler code here
	m_PhonicUSB.CloseDevice();
	int i;
	for(i=0;i<MAX_DEVICE;i++)
		ubox_device[i].	handl_plug_out();

   	m_list.DeleteAllItems();
	if(m_PhonicUSB.OpenDevice(0))
	{
		m_dis.InsertString(m_dis.GetCount(),"open device error");
		m_dis.SetCurSel(m_dis.GetCount()-1);   
	}


}

BEGIN_EVENTSINK_MAP(CUbox_recorder_testDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CUbox_recorder_testDlg)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 1 /* PlugOut */, OnPlugOut, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 2 /* PLugIn */, OnPLugIn, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 3 /* HookOff */, OnHookOff, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 4 /* HangUp */, OnHangUp, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 5 /* HangIng */, OnHangIng, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 6 /* Ring */, OnRing, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 7 /* CallId */, OnCallId, VTS_I4 VTS_BSTR VTS_BSTR VTS_BSTR)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 8 /* DtmfKey */, OnDtmfKey, VTS_I4 VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 9 /* RingCancel */, OnRingCancel, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 10 /* PlayEnd */, OnPlayEnd, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 11 /* PlayError */, OnPlayError, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 12 /* DeviceError */, OnDeviceError, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 13 /* DeviceAlarm */, OnDeviceAlarm, VTS_I4 VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 15 /* LineVoltage */, OnLineVoltage, VTS_I4 VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 16 /* StreamVoice */, OnStreamVoice, VTS_I4 VTS_PI2 VTS_I2)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 17 /* RingStop */, OnRingStop, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 18 /* ToneBusy */, OnToneBusy, VTS_I4)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 19 /* Polarity */, OnPolarity, VTS_I4 VTS_I2)
	ON_EVENT(CUbox_recorder_testDlg, IDC_PHONICUSBCTRL, 20 /* CallOutFinish */, OnCallOutFinish, VTS_I4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CUbox_recorder_testDlg::OnPlugOut(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,2,"plug out");
		pLine->handl_plug_out();
	}
}

void CUbox_recorder_testDlg::OnPLugIn(long uboxHandle) 
{
	char str[50] ;

	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(-1);
	if(pLine)
	{
		sprintf(str,"%d",m_list.GetItemCount()+1);
		
		pLine->handl_plug_in(uboxHandle,m_list.GetItemCount());
		int nn=m_list.InsertItem(m_list.GetItemCount(),str);
		m_list.SetItemText(m_list.GetItemCount()-1,2,"Plug In");
		int res = m_PhonicUSB.GetProductID(uboxHandle);
		if( res>0)
		{
			CString str;
			switch(res)
			{
			case 1:
				str = "FI3102A";
				break;
			case 2:
				str = "FI3002B";
				break;
			case 3:
				str = "FI3001B";
					break;
			case 4:
				str = "FI3101A";
				break;
			default:
				str = "unkown product";
			}
			m_list.SetItemText(m_list.GetItemCount()-1,1,str);
		}
	}
}

void CUbox_recorder_testDlg::OnHookOff(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,2,"hookoff");
		m_dis.InsertString(m_dis.GetCount(),"hook off");
		m_dis.SetCurSel(m_dis.GetCount()-1);
	}
	
}

void CUbox_recorder_testDlg::OnHangUp(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,2,"Hangup");
		pLine->clear_dtmf_key();
		m_dis.InsertString(m_dis.GetCount(),"OnHangup");
		m_dis.SetCurSel(m_dis.GetCount()-1);
	}
}

void CUbox_recorder_testDlg::OnHangIng(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,2,"Hang");
	}
}

void CUbox_recorder_testDlg::OnRing(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();

		m_dis.InsertString(m_dis.GetCount(),"ring");
		m_dis.SetCurSel(m_dis.GetCount()-1);

		m_list.SetItemText(lineID,2,"ring");
	}
}

void CUbox_recorder_testDlg::OnCallId(long uboxHandle, LPCTSTR callerNumber, LPCTSTR callerTime, LPCTSTR callerName) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,4,callerNumber);
	}
}

void CUbox_recorder_testDlg::OnDtmfKey(long uboxHandle, long dtmfCode) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		pLine->set_dtmf_key((char)dtmfCode);
		CString str = pLine->get_dtmfky();
		m_list.SetItemText(lineID,3,str);
		char buffer[10];
		sprintf(buffer,"%c",(char)dtmfCode);
		m_dis.InsertString(m_dis.GetCount(),buffer);
		m_dis.SetCurSel(m_dis.GetCount()-1);
	}
}

void CUbox_recorder_testDlg::OnRingCancel(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,2,"Miss call");
	}
}

void CUbox_recorder_testDlg::OnPlayEnd(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,5,"play end");
	}
}

void CUbox_recorder_testDlg::OnPlayError(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,5,"play error");
	}
}

void CUbox_recorder_testDlg::OnDeviceError(long uboxHandle) 
{
	MessageBox("device error,please reboot software,or plug out USB device!","prompt",MB_OK);
}

void CUbox_recorder_testDlg::OnDeviceAlarm(long uboxHandle, long param) 
{
	MessageBox("device alarm,please reboot software,or plug out USB device!","prompt",MB_OK);
}


void CUbox_recorder_testDlg::OnLineVoltage(long uboxHandle, long voltage) 
{
	char buff[100];
	sprintf(buff,"line voltage:%d V",voltage);
	m_dis.InsertString(m_dis.GetCount(),buff);
	m_dis.SetCurSel(m_dis.GetCount()-1);   
}

void CUbox_recorder_testDlg::OnStreamVoice(long uboxHandle, short FAR* pVoice, short size) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		if(pLine->fpstream)
		{
		   	int res = fwrite(pVoice,sizeof(char),size,pLine->fpstream);
			if(res != size )
			{
				m_dis.InsertString(m_dis.GetCount(),"stream data write file failed");
				m_dis.SetCurSel(m_dis.GetCount()-1);
			}
		}
	}
}

void CUbox_recorder_testDlg::OnRingStop(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,2,"ring stop");
	}
}

void CUbox_recorder_testDlg::OnToneBusy(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,5,"tone busy");
		int res = m_PhonicUSB.GetProductID(uboxHandle);
		  if((res == 1)||(res == 4) )
             m_PhonicUSB.SoftHookon(uboxHandle);
	}
	m_dis.InsertString(m_dis.GetCount(),"tone busy");
	m_dis.SetCurSel(m_dis.GetCount()-1);
}

void CUbox_recorder_testDlg::OnPolarity(long uboxHandle, short lineSign) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	CString str;
	if(lineSign)
		str = "polarity - ";
	else
	    str = "polarity + ";
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,5,str);
	}
	
	m_dis.InsertString(m_dis.GetCount(),str);
	m_dis.SetCurSel(m_dis.GetCount()-1);
}

void CUbox_recorder_testDlg::OnCallOutFinish(long uboxHandle) 
{
	CUBoxLine* pLine = NULL;
	pLine = get_line_by_handle(uboxHandle);
	if(pLine)
	{
		int lineID = pLine->line_id();
		m_list.SetItemText(lineID,5,"CallOutFinish");
	}
}

